//
//  SnapKitExtension.swift
//  suda
//
//  Created by youai on 2019/7/11.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    @discardableResult
    func drawImage(imageView imageView_:UIImageView? = nil ,image:UIImage?,
                   _ closure: (_ make: ConstraintMaker) -> Void
        )->UIImageView{
        let imageView = imageView_ ?? UIImageView()
        if image != nil {
            imageView.image = image
        }
        self.addSubview(imageView)
        imageView.snp.makeConstraints(closure)
        return imageView
    }
    
    @discardableResult
    func drawImage(imageView imageView_:UIImageView? = nil ,named:String?,
                   _ closure: (_ make: ConstraintMaker) -> Void
        )->UIImageView{
        let imageView = imageView_ ?? UIImageView()
        if named != nil {
            imageView.image = UIImage(named: named!)
        }
        self.addSubview(imageView)
        imageView.snp.makeConstraints(closure)
        return imageView
    }
    
    @discardableResult
    func drawLabel(label label_ : UILabel? = nil ,
                   text:String?,fontSize:CGFloat,fontWeight:UIFont.Weight = .regular ,textColor:UIColor,
                   _ closure: (_ make: ConstraintMaker) -> Void
        )->UILabel{
        let label = label_ ?? UILabel()
        label.text = text
        label.font = UIFont.systemFont(ofSize: fontSize, weight: fontWeight)
        label.textColor = textColor
        self.addSubview(label)
        label.snp.makeConstraints(closure)
        return label
    }
    
    @discardableResult
    func drawView(view view_ : UIView? = nil,
        _ closure: (_ make: ConstraintMaker) -> Void
        )->UIView{
        let view = view_ ?? UIView()
        self.addSubview(view)
        view.snp.makeConstraints(closure)
        return view
    }
    
    @discardableResult
    func drawButton(button button_:Button? = nil,
                    imageNamed:String, edgeInsets:UIEdgeInsets? = nil,
                    _ closure: (_ make: ConstraintMaker) -> Void
        )->Button{
        let button = button_ ?? Button()
        button.setImage(UIImage(named: imageNamed), for: .normal)
        if edgeInsets != nil {
            button.extraEdgeInsets = edgeInsets!
        }
        self.addSubview(button)
        button.snp.makeConstraints(closure)
        return button
    }
    
    @discardableResult
    func drawButton(button button_:Button? = nil,
                    title:String?, titleColor:UIColor,
                    fontSize:CGFloat, fontWeight:UIFont.Weight = .regular, edgeInsets:UIEdgeInsets? = nil,
                    _ closure: (_ make: ConstraintMaker) -> Void )->Button{
        let button = button_ ?? Button()
        button.setTitle(title, for: .normal)
        button.setTitleColor(titleColor, for: .normal)
        button.setTitleColor(.black, for: .highlighted)
        button.titleLabel!.font = UIFont.systemFont(ofSize: fontSize, weight: fontWeight)
        if edgeInsets != nil {
            button.extraEdgeInsets = edgeInsets!
        }
        self.addSubview(button)
        button.snp.makeConstraints(closure)
        return button
    }
    
    ///水平方向上适配周围的自动布局
    func horizontalFittingSize(){
        self.setContentHuggingPriority(.fittingSizeLevel , for: .horizontal)
        self.setContentCompressionResistancePriority(.fittingSizeLevel, for: .horizontal)
    }
    
    @discardableResult
    func drawTextField(textField textField_ : TextField? = nil,
                       placeholder:String?,fontSize:CGFloat,fontWeight:UIFont.Weight = .regular,
                       keyboardType:UIKeyboardType = .default,returnKeyType:UIReturnKeyType = .done ,
                       _ closure: (_ make: ConstraintMaker) -> Void)->TextField{
        let textField = textField_ ?? TextField()
        addSubview(textField)
        textField.placeholder = placeholder
        textField.font = UIFont.systemFont(ofSize: fontSize, weight: fontWeight)
        textField.keyboardType = keyboardType
        textField.returnKeyType = returnKeyType
        textField.snp.makeConstraints(closure)
        return textField
    }
    
    
}


enum FieldAlign {
    case left
    case right
    case centerOfLeft
    case centerOfRight
}

extension UIView {
    
    ///按照相对Superview的左右位置绘制Label
    func _drawField(label label_ : UILabel? = nil ,
                   align : FieldAlign,offset: CGFloat, textWidth: CGFloat = 0, space : CGFloat = 5,
                   text:String?,fontSize:CGFloat,fontWeight:UIFont.Weight = .regular ,textColor:UIColor,
                   _ closure: (_ make: ConstraintMaker) -> Void
        )-> UILabel{
        let label = self.drawLabel(label: label_, text: text, fontSize: fontSize, fontWeight: fontWeight, textColor: textColor) { make in
            switch align {
            case .left:
                make.left.equalToSuperview().offset(offset)
                make.left.greaterThanOrEqualToSuperview().offset(space).priority(.high)
            case .right:
                make.right.equalToSuperview().offset(-offset)
                make.right.lessThanOrEqualToSuperview().offset(-space).priority(.high)
            case .centerOfLeft:
                make.centerX.equalTo(self.snp.left).offset(offset+textWidth*0.5)
            case .centerOfRight:
                make.centerX.equalTo(self.snp.right).offset(-offset-textWidth*0.5)
            }
            closure(make)
        }
        return label
    }
    
    ///在一个view的正右侧绘制Label
    @discardableResult
    func drawRightOf(label : UILabel? = nil ,
                   target:UIView,space:CGFloat = 0,
                   text:String?,fontSize:CGFloat,fontWeight:UIFont.Weight = .regular ,textColor:UIColor
        )-> UILabel {
        let label = self.drawLabel(label: label, text: text, fontSize: fontSize, fontWeight: fontWeight, textColor: textColor) { make in
            make.left.equalTo(target.snp.right).offset(space)
            make.centerY.equalTo(target)
        }
        return label
    }
    
    ///绘制左右连接在一起的两个Label，并且用左边距和centerY定位
    func drawDoubleLabel(leftOffset:CGFloat,centerY:CGFloat,adjustHeight:CGFloat,
                         leftLabel : UILabel? = nil , leftText:String?,leftFontSize:CGFloat,leftFontWeight:UIFont.Weight = .regular ,leftTextColor:UIColor,
                         rightLabel : UILabel? = nil , rightText:String?,rightFontSize:CGFloat,rightFontWeight:UIFont.Weight = .regular ,rightTextColor:UIColor,
                         space: CGFloat = 0
        ){
        let _leftLabel = drawLabel(label: leftLabel, text: leftText, fontSize: leftFontSize, fontWeight: leftFontWeight, textColor: leftTextColor) { (make) in
            make.left.equalToSuperview().offset(leftOffset)
            make.centerY.equalTo(self.snp.top).offset(centerY+adjustHeight*0.5)
        }
        drawLabel(label: rightLabel, text: rightText, fontSize: rightFontSize, fontWeight: rightFontWeight, textColor: rightTextColor) { (make) in
            make.left.equalTo(_leftLabel.snp.right).offset(space)
            make.centerY.equalTo(_leftLabel)
        }
    }
    

}
